package com.fs.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.mapper.OrderGoodsMapper;
import com.fs.mapper.OrderGoodsMapper;
import com.fs.pojo.OrderGoods;
import com.fs.pojo.OrderGoods;
import com.fs.service.OrderGoodsService;
import com.fs.vo.CommRequestVO;
import com.fs.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderGoodsServiceImpl implements OrderGoodsService {
    @Autowired
    private OrderGoodsMapper orderGoodsMapper;


    @Override
    @Transactional
    public ResultVO add(OrderGoods entity) {
        orderGoodsMapper.insert(entity);
        return ResultVO.success();
    }

    @Override
    @Transactional
    public ResultVO edit(OrderGoods entity) {
        orderGoodsMapper.updateById(entity);
        return ResultVO.success();
    }

    @Override
    public ResultVO list(CommRequestVO request) {
        if(request==null)return ResultVO.fail();

        int pageNum = request.getPageNum();
        int pageSize = request.getPageSize();

        if(pageNum<1)pageNum=1;
        if(pageSize<1)pageNum=10;

        Page<OrderGoods> page = new Page(pageNum, pageSize);


        QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper();
        queryWrapper.like(StrUtil.isNotBlank(request.getQuery()),"username",request.getQuery())
                .orderByDesc("id");
        page=orderGoodsMapper.selectPage(page,queryWrapper);
        request.setTotal(page.getTotal()).setRows(page.getRecords());

        return ResultVO.success(request);
    }

    @Override
    @Transactional
    public ResultVO delById(Integer id) {
        orderGoodsMapper.deleteById(id);
        return ResultVO.success();
    }
}
